package cn.car91.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import cn.car91.R;
import cn.car91.adapter.UserCommentAdapter;
import cn.car91.bean.ArticleComment;
import cn.car91.bean.ExposureComment;
import cn.car91.bean.RepairShopComment;
import cn.car91.pager.CommentPager;
import cn.car91.utils.Car91JsonUtil;
import cn.car91.view.VerticalScrollView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnItemClick;
import com.lidroid.xutils.view.annotation.event.OnRadioGroupCheckedChange;

public class UserCommentActivity extends BaseActivity {

    private CommentPager commentPager = new CommentPager();

    @ViewInject(R.id.comment_list_scroll)
    VerticalScrollView commentScrollView;
    @ViewInject(R.id.comment_list)
    ListView commentView;
    @ViewInject(R.id.none)
    View none;

    UserCommentAdapter commentAdapter = null;
    private boolean addDate = false;
    String type = "repairshop";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        setContentView(R.layout.activity_user_comment);
        super.onCreate(savedInstanceState);

        commentAdapter = new UserCommentAdapter(this, R.layout.view_user_comment);
        commentView.setAdapter(commentAdapter);
        getData();
        commentScrollView.setOnTouchListener(onScrollTouchListener);
        ((TextView) findViewById(R.id.header_text)).setText("我的评论");
    }

    @OnRadioGroupCheckedChange(R.id.comment_radio)
    public void onRadioCheckedChanged(RadioGroup arg0, int arg1) {
        // TODO Auto-generated method stub
        switch (arg1) {
            case R.id.comment_repairshop:
                type = "repairshop";
                break;
            case R.id.comment_article:
                type = "article";
                break;
            case R.id.comment_exposure:
                type = "exposure";
                break;
            default:
                break;
        }
        addDate = false;
        commentPager.reset();
        getData();
    }

    private OnTouchListener onScrollTouchListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            switch (arg1.getAction()) {
                case MotionEvent.ACTION_UP:
                    doOnBorderListener();
                    break;
            }
            return false;
        }

        private void doOnBorderListener() {
            if (commentView != null && commentView.getMeasuredHeight() <= commentScrollView.getScrollY() + commentScrollView.getHeight()) {
                //底部
                addDate = true;
                getData();
            } else if (commentScrollView.getScrollY() == 0) {
                //顶部
            }
        }
    };

    public void getData() {
    	commentPager.getUserComment(getCar91Application().getUser().getId(), type, callbackList);
    }

    /* 列表页面 */
    private void refreshListViewData(List data) {
        if (addDate) {
            commentAdapter.addData(data);
        } else {
            commentView.removeAllViewsInLayout();
            commentAdapter.setData(data);
        }
        commentAdapter.notifyDataSetChanged();
        addDate = true;
    }

    /**
     * 回调获取商家列表
     */
    private RequestCallBack callbackList = new RequestCallBack() {

        @Override
        public void onStart() {
            // TODO Auto-generated method stub
            super.onStart();

            showTextWhenNone(false);
        }

        @Override
        public void onFailure(HttpException arg0, String arg1) {
            // TODO Auto-generated method stub
            Toast.makeText(UserCommentActivity.this, "网络异常~", Toast.LENGTH_SHORT).show();

            showTextWhenNone(false);
        }

        @Override
        public void onSuccess(ResponseInfo arg0) {
            // TODO Auto-generated method stub
            List comments = new ArrayList();
            if (type.equals("repairshop")) {
                comments = Car91JsonUtil.getDTOList(arg0.result.toString(), RepairShopComment.class);
            } else if (type.equals("article")) {
                comments = Car91JsonUtil.getDTOList(arg0.result.toString(), ArticleComment.class);
            } else if (type.equals("exposure")) {
                comments = Car91JsonUtil.getDTOList(arg0.result.toString(), ExposureComment.class);
            }
            if (comments.size() > 0) {
                showTextWhenNone(false);
            } else if (!addDate) {
                showTextWhenNone(true);
            }
            refreshListViewData(comments);
        }

        private void showTextWhenNone(Boolean show) {
            if (show) {
                if (commentScrollView.getVisibility() == View.VISIBLE) {
                    commentScrollView.setVisibility(View.GONE);
                }
                if (none.getVisibility() == View.GONE) {
                    none.setVisibility(View.VISIBLE);
                }
            } else {
                if (commentScrollView.getVisibility() == View.GONE) {
                    commentScrollView.setVisibility(View.VISIBLE);
                }
                if (none.getVisibility() == View.VISIBLE) {
                    none.setVisibility(View.GONE);
                }
            }
        }
    };

    @OnItemClick(R.id.comment_list)
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                            long arg3) {
        TextView idView = (TextView) arg1.findViewById(R.id.item_id);
        String id = idView.getText().toString();

        Intent intent = new Intent();
        if (type.equals("repairshop")) {
            intent.putExtra("rid", id);
            intent.setClass(UserCommentActivity.this, RepairShopDetailActivity.class);
        } else if (type.equals("article")) {
            intent.putExtra("aid", id);
            intent.setClass(UserCommentActivity.this, ArticleDetailActivity.class);
        } else if (type.equals("exposure")) {
            intent.putExtra("eid", id);
            intent.setClass(UserCommentActivity.this, ExposureDetailActivity.class);
        }

        startActivity(intent);
    }
}
